package solved;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/*
ID:		liluyan1
LANG:	JAVA
TASK:	prefix
*/
public class prefix {
	public static void main(String[] args) throws IOException {
		BufferedReader f = new BufferedReader(new FileReader("prefix.in"));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
				"prefix.out")));
		new prefix().solve(f, out);
		out.close();
		System.exit(0);
	}
	
	private ArrayList<String> pList;
	private String file;
	
	private void solve(BufferedReader f, PrintWriter out) throws IOException {
		pList = new ArrayList<String>();
		while (true) {
			String line = f.readLine();
			if (".".equals(line)) break;
			String[] temp = line.split(" ");
			for (String s : temp) pList.add(s);
		}
		Collections.sort(pList, new Comparator<String>(){
			public int compare(String a, String b) {
				return b.length() - a.length();
			}
		});
		
		StringBuilder sb = new StringBuilder();
		while (true) {
			String line = f.readLine();
			if (line==null || line.length() == 0) break;
			sb.append(line);
		}
		file = sb.toString();
		int size = file.length();
		
		int L = file.length();
		boolean[] reach = new boolean[L+1];
		
		int max = 0;
		ArrayDeque<Integer> queue = new ArrayDeque<Integer>();
		queue.add(0);
		while (!queue.isEmpty()) {
			int pos = queue.remove();
			for (String prefix : pList) {
				int end = pos + prefix.length();
				if (end > size || reach[end]) continue;
				if (prefix.equals(file.substring(pos, end))) {
					reach[end] = true;
					queue.add(end);
					max = Math.max(max, end);
				}
			}
		}
		
		out.println(max);
	}
	
	public int read(BufferedReader f) throws IOException {
		return Integer.parseInt(f.readLine());
	}
	
	public int[] read(BufferedReader f, int N) throws IOException {
		String[] t = f.readLine().split(" ");
		int[] a = new int[N];
		for (int i=0; i<N; i++) {
			a[i] = Integer.parseInt(t[i]);
		}
		return a;
	}
}
